# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# 2.0.7 - This version does NOT support Python 2.x so we have to have
# a separate package for django 1.11.13 for Python2.
#
_realname=django
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.2.6
pkgrel=1
pkgdesc="A high-level Python Web framework that encourages rapid development and clean design (mingw-w64)"
arch=('any')
url='https://www.djangoproject.com/'
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-bcrypt"
             "${MINGW_PACKAGE_PREFIX}-python3-argon2_cffi"
             "${MINGW_PACKAGE_PREFIX}-python3-pytz"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
options=('staticlibs' 'strip' '!debug')
source=("Django-${pkgver}.tar.gz"::"https://www.djangoproject.com/download/${pkgver}/tarball/")
sha512sums=('1bd44543ea06b6c412b1c15a6850c4aa68b637b360c922cc6ed06e7e0b2f22142d94c8c455f5c24b8c34fa5d431b5a3d80502928b5445bc2c36a5458bd5eb627')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  rm -rf python3-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python3-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {
  msg "Python 3 build for ${CARCH}"  
  cd "${srcdir}/python3-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-pytz")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-psycopg2: for PostgreSQL backend"
              "${MINGW_PACKAGE_PREFIX}-python3-bcrypt: for bcrypt support"
              "${MINGW_PACKAGE_PREFIX}-python3-argon2_cffi: for Argon2 support")
  install=${_realname}3-${CARCH}.install

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"

# This entire section should be removed if the package does NOT install
# anything in the /mingw*/bin directory.
### begin section ###
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|${MINGW_PREFIX}/bin/|g" -i ${_f}
  done
#### end section ####

# for Python3 packages, you want to copy the bin files to something with 3
# as a suffix for consistancy with Archlinux's symlink.
#  ln -s django-admin.py "$pkgdir"/usr/bin/django-admin3.py
#  ln -s django-admin "$pkgdir"/usr/bin/django-admin3

  for f in django-admin; do
    cp "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,3}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      cp "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,3}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}3.exe.manifest
    fi
    cp "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,3}.py
    cp "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,3}-script.py
  done
#### end section ####
}
